/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ldtwo.flashcard;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.ldtwo.GoTTS.G;
import org.ldtwo.GoTTS.MainFrame;
import org.ldtwo.GoTTS.AudioPlayer;
import static org.ldtwo.flashcard.FlashCardFrame.path;

/**
 *
 * @author ldtwo
 */
public class QuizPanel extends ReviewPanel {

    final HashMap<String, String> rules;
    int image_count = 2;
    int num_options = 4;

    class Action extends MouseAdapter implements Runnable, KeyListener {

        @Override
        public void run() {
        }

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }

    }

    Action action = new Action() {

        @Override
        public void mouseEntered(MouseEvent e) {
            //player.playMP3(null);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getSource() == buttons[0]) {//right     
                //score++
                //play btn[0]
            } else {//wrong
                if (chances-- > 0) {
                    return;
                } else {
                    //fall through
                }
            }
            //play btn[0]
            //score--
            buttons[0].setBackground(Color.green);
            for (int i = 1; i < buttons.length; i++) {
                buttons[i].setBackground(Color.gray);
            }
        }

        @Override
        public void keyPressed(KeyEvent e) {

        }
    };

    /**
     *
     * @param f2
     * @param hostFrame
     * @param hostContainer
     * @param inputFile
     * @param leftLanguage
     * @param rightLanguage
     * @param rules
     * @throws Exception
     */
    public QuizPanel(MainFrame f2, JFrame hostFrame, Container hostContainer, File inputFile,
            String leftLanguage, String rightLanguage, HashMap<String, String> rules) throws Exception {
        super(f2, hostFrame, hostContainer, inputFile, leftLanguage, rightLanguage);
        this.rules = rules;

        panAnswers.setLayout(new FlowLayout());

        rules.put("left-audio", "");
        rules.put("right-audio", "");
        rules.put("left-text", "");
        rules.put("right-text", "");
        rules.put("right-image", "");
        rules.put("left-image", "");
        rules.put("image-count", "4");
        rules.put("num-options", "4");

        image_count = getInt("image-count", image_count);
        num_options = getInt("num-options", num_options);

        System.out.println("image_count=" + image_count);
        System.out.println("num_options=" + num_options);
    }

    final public int getInt(String name, int defaul) {
        try {
            int i = Integer.parseInt(rules.get(name));
            return i > 0 ? i : defaul;
        } catch (Exception exception) {
            return defaul;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(0, 0, 0));
        setLayout(new java.awt.GridBagLayout());
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    public void print(Term t) {
        System.out.print(t.toString());
    }

    public void show(Term t) {
        //super.show(t);
        //if(true)return;
        System.out.printf(":: %s\n", t);
        if (t == null) {
            return;
        }

        int pos = shuffledDeck.indexOf(t);
        question.setText(useFront ? t.left : t.right);
        startTime = System.currentTimeMillis();
        gbc.weightx = 1;
        gbc.weighty = 1;
        System.out.printf(":: %s\n", pos);
        System.out.printf("num_options:: %s\n", num_options);
        System.out.printf("image_count:: %s\n", image_count);
        LinkedList<JPanel> panels = new LinkedList<>();
        for (int group = pos; group < num_options + pos; group++) {
            Term term = shuffledDeck.get((group % shuffledDeck.size()));
            JPanel pan = new JPanel(new FlowLayout());
            panels.add(pan);
            int cnt = 0;
            HashSet<ImageFile> set = new HashSet<>();
            for (ImageFile imgf : term.getLeftSet()) {
                try {
                    if (imgf.enabled) {
                        imgf.button = loadButton(imgf);
                        if (imgf.button == null) {
                            imgf.enabled = false;
                        } else {

                            if (cnt++ < image_count) {
                                set.add(imgf);
                            }
                        }
                    }
                } catch (Exception ex) {
                    System.err.println(imgf);
                    ex.printStackTrace();
                }
            }
            for (ImageFile imgf : term.getRightSet()) {
                try {
                    if (imgf.enabled) {
                        imgf.button = loadButton(imgf);
                        if (imgf.button == null) {
                            imgf.enabled = false;
                        } else {
                            if (cnt++ < image_count) {
                                set.add(imgf);
                            }
                        }
                    }
                } catch (Exception e) {
                    System.err.println(e);
                }
            }
            for (ImageFile imgf : set) {
                pan.add(imgf.button);
            }

            pan.updateUI();
            pan.setPreferredSize(new Dimension(question.getWidth() / (num_options + 1), 600));

        }
        Collections.shuffle(panels);
        for (JPanel pan : panels) {
            panAnswers.add(pan);
        }
        playAudio(t);
    }

    private AbstractButton loadButton(ImageFile imgf) throws IOException {

        int width = question.getWidth() / 11;

        return ImageManager.imageFile2jButton_check(width, width * 3 / 4, imgf.file);

    }

    public void updateScore() {

    }
}
